home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / malloc_str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  13.1 KB  |  465 lines

  1. /*
  2.  * functions for testing of string routines arguments.
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * This file contains functions to be used to verify the arguments of
  27.  * string functions.   If enabled these can discover problems with
  28.  * heap-based strings (such as fence errors) much closer to the error.
  29.  */
  30.  
  31. #define MALLOC_DEBUG_DISABLE
  32.  
  33. #include "malloc.h"
  34. #include "malloc_loc.h"
  35.  
  36. #include "conf.h"
  37. #include "chunk.h"
  38. #include "dbg_values.h"
  39. #include "error.h"
  40. #include "malloc_str.h"
  41.  
  42. #if INCLUDE_RCS_IDS
  43. LOCAL    char    *rcs_id =
  44.   "$Id: malloc_str.c,v 1.3 1993/04/06 04:24:41 gray Exp $";
  45. #endif
  46.  
  47. #if HAVE_BCMP
  48. /*
  49.  * dummy function for checking bcmp's arguments.
  50.  */
  51. EXPORT    int    _malloc_bcmp(void * b1, void * b2, int len)
  52. {
  53.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  54.     if (_chunk_pnt_check("bcmp", b1, 1, len) != NOERROR
  55.     || _chunk_pnt_check("bcmp", b2, 1, len) != NOERROR)
  56.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  57.     _malloc_message("bad pointer argument found in bcmp");
  58.   }
  59.   return bcmp(b1, b2, len);
  60. }
  61. #endif
  62.  
  63. #if HAVE_BCOPY
  64. /*
  65.  * dummy function for checking bcopy's arguments.
  66.  */
  67. EXPORT    void    _malloc_bcopy(char * from, char * to, int len)
  68. {
  69.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  70.     if (_chunk_pnt_check("bcopy", from, 1, len) != NOERROR
  71.     || _chunk_pnt_check("bcopy", to, 1, len) != NOERROR)
  72.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  73.     _malloc_message("bad pointer argument found in bcopy");
  74.   }
  75.   bcopy(from, to, len);
  76. }
  77. #endif
  78.  
  79. #if HAVE_MEMCMP
  80. /*
  81.  * dummy function for checking memcmp's arguments.
  82.  */
  83. EXPORT    int    _malloc_memcmp(void * b1,
  84.                    void * b2, int len)
  85. {
  86.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  87.     if (_chunk_pnt_check("memcmp", b1, 1, len) != NOERROR
  88.     || _chunk_pnt_check("memcmp", b2, 1, len) != NOERROR)
  89.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  90.     _malloc_message("bad pointer argument found in memcmp");
  91.   }
  92.   return memcmp(b1, b2, len);
  93. }
  94. #endif
  95.  
  96. #if HAVE_MEMCPY
  97. /*
  98.  * dummy function for checking memcpy's arguments.
  99.  */
  100. EXPORT    char    *_malloc_memcpy(char * to, char * from, int len)
  101. {
  102.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  103.     if (_chunk_pnt_check("memcpy", to, 1, len) != NOERROR
  104.     || _chunk_pnt_check("memcpy", from, 1, len) != NOERROR)
  105.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  106.     _malloc_message("bad pointer argument found in memcpy");
  107.   }
  108.   return (char *)memcpy(to, from, len);
  109. }
  110. #endif
  111.  
  112. #if HAVE_MEMSET
  113. /*
  114.  * dummy function for checking memset's arguments.
  115.  */
  116. EXPORT    char    *_malloc_memset(void * buf, char ch, int len)
  117. {
  118.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  119.     if (_chunk_pnt_check("memset", buf, 1, len) != NOERROR)
  120.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  121.     _malloc_message("bad pointer argument found in memset");
  122.   }
  123.   return (char *)memset(buf, ch, len);
  124. }
  125. #endif
  126.  
  127. #if HAVE_INDEX
  128. /*
  129.  * dummy function for checking index's arguments.
  130.  */
  131. EXPORT    char    *_malloc_index(char * str, char ch)
  132. {
  133.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  134.     if (_chunk_pnt_check("index", str, 1, 0) != NOERROR)
  135.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  136.     _malloc_message("bad pointer argument found in index");
  137.   }
  138.   return (char *)index(str, ch);
  139. }
  140. #endif
  141.  
  142. #if HAVE_RINDEX
  143. /*
  144.  * dummy function for checking rindex's arguments.
  145.  */
  146. EXPORT    char    *_malloc_rindex(char * str, char ch)
  147. {
  148.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  149.     if (_chunk_pnt_check("rindex", str, 1, 0) != NOERROR)
  150.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  151.     _malloc_message("bad pointer argument found in rindex");
  152.   }
  153.   return (char *)rindex(str, ch);
  154. }
  155. #endif
  156.  
  157. #if HAVE_STRCAT
  158. /*
  159.  * dummy function for checking strcat's arguments.
  160.  */
  161. EXPORT    char    *_malloc_strcat(char * to, char * from)
  162. {
  163.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  164.     /* maybe should check to, strlen(to) + strlen(from) */
  165.     if (_chunk_pnt_check("strcat", to, 1, 0) != NOERROR
  166.     || _chunk_pnt_check("strcat", from, 1, 0) != NOERROR)
  167.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  168.     _malloc_message("bad pointer argument found in strcat");
  169.   }
  170.   return (char *)strcat(to, from);
  171. }
  172. #endif
  173.  
  174. #if HAVE_STRCMP
  175. /*
  176.  * dummy function for checking strcmp's arguments.
  177.  */
  178. EXPORT    int    _malloc_strcmp(char * s1,
  179.                    char * s2)
  180. {
  181.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  182.     if (_chunk_pnt_check("strcmp", s1, 1, 0) != NOERROR
  183.     || _chunk_pnt_check("strcmp", s2, 1, 0) != NOERROR)
  184.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  185.     _malloc_message("bad pointer argument found in strcmp");
  186.   }
  187.   return strcmp(s1, s2);
  188. }
  189. #endif
  190.  
  191. #if HAVE_STRLEN
  192. /*
  193.  * dummy function for checking strlen's arguments.
  194.  */
  195. EXPORT    int    _malloc_strlen(char * str)
  196. {
  197.   if (BIT_IS_SET(_malloc_debug,
  198.          DEBUG_CHECK_FUNCS)) {
  199.     if (_chunk_pnt_check("strlen", str, 1, 0) != NOERROR)
  200.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  201.     _malloc_message("bad pointer argument found in strlen");
  202.   }
  203.   return strlen(str);
  204. }
  205. #endif
  206.  
  207. #if HAVE_STRTOK
  208. /*
  209.  * dummy function for checking strtok's arguments.
  210.  */
  211. EXPORT    char    *_malloc_strtok(char * str, char * sep)
  212. {
  213.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  214.     if (_chunk_pnt_check("strtok", str, 1, 0) != NOERROR
  215.     || _chunk_pnt_check("strtok", sep, 1, 0) != NOERROR)
  216.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  217.     _malloc_message("bad pointer argument found in strtok");
  218.   }
  219.   return (char *)strtok(str, sep);
  220. }
  221. #endif
  222.  
  223. #if HAVE_BZERO
  224. /*
  225.  * dummy function for checking bzero's arguments.
  226.  */
  227. EXPORT    void    _malloc_bzero(void * buf,
  228.                   int len)
  229. {
  230.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  231.     if (_chunk_pnt_check("bzero", buf, 1, len) != NOERROR)
  232.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  233.     _malloc_message("bad pointer argument found in bzero");
  234.   }
  235.   bzero(buf, len);
  236. }
  237. #endif
  238.  
  239. #if HAVE_MEMCCPY
  240. /*
  241.  * dummy function for checking memccpy's arguments.
  242.  */
  243. EXPORT    char    *_malloc_memccpy(char * s1, char * s2, char ch, int len)
  244. {
  245.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  246.     /* maybe len maybe first ch */
  247.     if (_chunk_pnt_check("memccpy", s1, 1, 0) != NOERROR
  248.     || _chunk_pnt_check("memccpy", s2, 1, 0) != NOERROR)
  249.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  250.     _malloc_message("bad pointer argument found in memccpy");
  251.   }
  252.   return (char *)memccpy(s1, s2, ch, len);
  253. }
  254. #endif
  255.  
  256. #if HAVE_MEMCHR
  257. /*
  258.  * dummy function for checking memchr's arguments.
  259.  */
  260. EXPORT    char    *_malloc_memchr(char * s1, char ch, int len)
  261. {
  262.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  263.     if (_chunk_pnt_check("memchr", s1, 1, len) != NOERROR)
  264.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  265.     _malloc_message("bad pointer argument found in memchr");
  266.   }
  267.   return (char *)memchr(s1, ch, len);
  268. }
  269. #endif
  270.  
  271. #if HAVE_STRCHR
  272. /*
  273.  * dummy function for checking strchr's arguments.
  274.  */
  275. EXPORT    char    *_malloc_strchr(char * str, char ch)
  276. {
  277.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  278.     if (_chunk_pnt_check("strchr", str, 1, 0) != NOERROR)
  279.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  280.     _malloc_message("bad pointer argument found in strchr");
  281.   }
  282.   return (char *)strchr(str, ch);
  283. }
  284. #endif
  285.  
  286. #if HAVE_STRRCHR
  287. /*
  288.  * dummy function for checking strrchr's arguments.
  289.  */
  290. EXPORT    char    *_malloc_strrchr(char * str, char ch)
  291. {
  292.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  293.     if (_chunk_pnt_check("strrchr", str, 1, 0) != NOERROR)
  294.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  295.     _malloc_message("bad pointer argument found in strrchr");
  296.   }
  297.   return (char *)strrchr(str, ch);
  298. }
  299. #endif
  300.  
  301. #if HAVE_STRCPY
  302. /*
  303.  * dummy function for checking strcpy's arguments.
  304.  */
  305. EXPORT    char    *_malloc_strcpy(char * to, char * from)
  306. {
  307.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  308.     /* maybe to, strlen(from) */
  309.     if (_chunk_pnt_check("strcpy", to, 1, 0) != NOERROR
  310.     || _chunk_pnt_check("strcpy", from, 1, 0) != NOERROR)
  311.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  312.     _malloc_message("bad pointer argument found in strcpy");
  313.   }
  314.   return (char *)strcpy(to, from);
  315. }
  316. #endif
  317.  
  318. #if HAVE_STRNCPY
  319. /*
  320.  * dummy function for checking strncpy's arguments.
  321.  */
  322. EXPORT    char    *_malloc_strncpy(char * to, char * from, int len)
  323. {
  324.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  325.     /* len or until nullc */
  326.     if (_chunk_pnt_check("strncpy", to, 1, 0) != NOERROR
  327.     || _chunk_pnt_check("strncpy", from, 1, 0) != NOERROR)
  328.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  329.     _malloc_message("bad pointer argument found in strncpy");
  330.   }
  331.   return (char *)strncpy(to, from, len);
  332. }
  333. #endif
  334.  
  335. #if HAVE_STRCASECMP
  336. /*
  337.  * dummy function for checking strcasecmp's arguments.
  338.  */
  339. EXPORT    int    _malloc_strcasecmp(char * s1, char * s2)
  340. {
  341.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  342.     if (_chunk_pnt_check("strcasecmp", s1, 1, 0) != NOERROR
  343.     || _chunk_pnt_check("strcasecmp", s2, 1, 0) != NOERROR)
  344.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  345.     _malloc_message("bad pointer argument found in strcasecmp");
  346.   }
  347.   return strcasecmp(s1, s2);
  348. }
  349. #endif
  350.  
  351. #if HAVE_STRNCASECMP
  352. /*
  353.  * dummy function for checking strncasecmp's arguments.
  354.  */
  355. EXPORT    int    _malloc_strncasecmp(char * s1, char * s2, int len)
  356. {
  357.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  358.     /* len or until nullc */
  359.     if (_chunk_pnt_check("strncasecmp", s1, 1, 0) != NOERROR
  360.     || _chunk_pnt_check("strncasecmp", s2, 1, 0) != NOERROR)
  361.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  362.     _malloc_message("bad pointer argument found in strncasecmp");
  363.   }
  364.   return strncasecmp(s1, s2, len);
  365. }
  366. #endif
  367.  
  368. #if HAVE_STRSPN
  369. /*
  370.  * dummy function for checking strspn's arguments.
  371.  */
  372. EXPORT    int    _malloc_strspn(char * str, char * list)
  373. {
  374.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  375.     if (_chunk_pnt_check("strspn", str, 1, 0) != NOERROR
  376.     || _chunk_pnt_check("strspn", list, 1, 0) != NOERROR)
  377.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  378.     _malloc_message("bad pointer argument found in strspn");
  379.   }
  380.   return strspn(str, list);
  381. }
  382. #endif
  383.  
  384. #if HAVE_STRCSPN
  385. /*
  386.  * dummy function for checking strcspn's arguments.
  387.  */
  388. EXPORT    int    _malloc_strcspn(char * str, char * list)
  389. {
  390.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  391.     if (_chunk_pnt_check("strcspn", str, 1, 0) != NOERROR
  392.     || _chunk_pnt_check("strcspn", list, 1, 0) != NOERROR)
  393.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  394.     _malloc_message("bad pointer argument found in strcspn");
  395.   }
  396.   return strcspn(str, list);
  397. }
  398. #endif
  399.  
  400. #if HAVE_STRNCAT
  401. /*
  402.  * dummy function for checking strncat's arguments.
  403.  */
  404. EXPORT    char    *_malloc_strncat(char * to, char * from, int len)
  405. {
  406.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  407.     /* either len or nullc */
  408.     if (_chunk_pnt_check("strncat", to, 1, 0) != NOERROR
  409.     || _chunk_pnt_check("strncat", from, 1, 0) != NOERROR)
  410.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  411.     _malloc_message("bad pointer argument found in strncat");
  412.   }
  413.   return (char *)strncat(to, from, len);
  414. }
  415. #endif
  416.  
  417. #if HAVE_STRNCMP
  418. /*
  419.  * dummy function for checking strncmp's arguments.
  420.  */
  421. EXPORT    int    _malloc_strncmp(char * s1, char * s2, int len)
  422. {
  423.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  424.     /* either len or nullc */
  425.     if (_chunk_pnt_check("strncmp", s1, 1, 0) != NOERROR
  426.     || _chunk_pnt_check("strncmp", s2, 1, 0) != NOERROR)
  427.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  428.     _malloc_message("bad pointer argument found in strncmp");
  429.   }
  430.   return strncmp(s1, s2, len);
  431. }
  432. #endif
  433.  
  434. #if HAVE_STRPBRK
  435. /*
  436.  * dummy function for checking strpbrk's arguments.
  437.  */
  438. EXPORT    char    *_malloc_strpbrk(char * str, char * list)
  439. {
  440.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  441.     if (_chunk_pnt_check("strpbrk", str, 1, 0) != NOERROR
  442.     || _chunk_pnt_check("strpbrk", list, 1, 0) != NOERROR)
  443.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  444.     _malloc_message("bad pointer argument found in strpbrk");
  445.   }
  446.   return (char *)strpbrk(str, list);
  447. }
  448. #endif
  449.  
  450. #if HAVE_STRSTR
  451. /*
  452.  * dummy function for checking strstr's arguments.
  453.  */
  454. EXPORT    char    *_malloc_strstr(char * str, char * pat)
  455. {
  456.   if (BIT_IS_SET(_malloc_debug, DEBUG_CHECK_FUNCS)) {
  457.     if (_chunk_pnt_check("str", str, 1, 0) != NOERROR
  458.     || _chunk_pnt_check("str", pat, 1, 0) != NOERROR)
  459.       if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_BAD_POINTER))
  460.     _malloc_message("bad pointer argument found in strstr");
  461.   }
  462.   return (char *)strstr(str, pat);
  463. }
  464. #endif
  465.